import time
from telnetlib import EC

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.Util import BlogDriver

class BlogDetail:
    driver = ""
    url = ""
    def __init__(self):
        self.driver = BlogDriver.driver
        # 一篇博客的url
        self.url = "http://8.137.19.140:9090/blog_detail.html?blogId=122533"
        self.driver.get(self.url)

    def BlogLoginDetailTest(self):
        # 检查头像
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > img")
        # 检查名称
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        # 检查GitHub连接
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > a")
        # 检查文章
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
        # 检查文章数量
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(5) > span:nth-child(1)")
        # 检查分类
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")
        # 检查分类数量
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(5) > span:nth-child(2)")
        # 检查详情区博客标题
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.title")
        # 检查详情区博客发布时间
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.date")
        # 检查详情区博客正文
        self.driver.find_element(By.CSS_SELECTOR,"#detail > p > strong")
        # 检查详情区编辑按钮
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(1)")
        # 检查详情区删除按钮
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)")

        # 截图
        BlogDriver.GetScreenShot()
        print("详情页元素检查无误")
        time.sleep(1)

    def NotBlogLoginDetailTest(self):
        # 未登录状态下会跳转回登录页面，检查是否回到登录页面即可
        self.driver.get("http://8.137.19.140:9090/blog_detail.html?blogId=122533")
        time.sleep(2)
        BlogDriver.GetScreenShot()
        # 检查到看登录框说明访问后跳转回登录页面
        # 判断一下退出后的页面url和页面的标题
        Quittitle = self.driver.title
        QuitUrl = self.driver.current_url
        print(Quittitle)
        print(QuitUrl)
        assert Quittitle == "博客登陆页"
        assert QuitUrl == "http://8.137.19.140:9090/blog_login.html"
        print("检查到回到登录页面")
        self.driver.quit()

    # 注销
    def QuitBlogLoginListText(self):  # 登录博客状态下测试退出
        # 点击退出按钮
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()
        BlogDriver.GetScreenShot()

        # 判断一下退出后的页面url和页面的标题
        Quittitle = self.driver.title
        QuitUrl = self.driver.current_url
        print(Quittitle)
        print(QuitUrl)
        assert Quittitle == "博客登陆页"
        assert QuitUrl == "http://8.137.19.140:9090/blog_login.html"

        # 判断一下账号输入框和密码输入框的内容是否为空
        Quitusername = self.driver.find_element(By.CSS_SELECTOR, "#username").text
        Quitpassword = self.driver.find_element(By.CSS_SELECTOR, "#password").text
        print("Quitusername" + Quitusername)
        print("Quitpassword" + Quitpassword)
        assert Quitusername == ""
        assert Quitpassword == ""
        print("注销成功")

    # 删除博客
    def DeleteBlogLoginDetailTest(self):
        # 登录后进入到自己的一篇博客
        # 此时进入到了详情页面
        BlogDriver.GetScreenShot()
        # 点击删除按钮
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)").click()
        # 点击删除按钮后会出现一个弹窗
        # wait = WebDriverWait(self.driver, 2)
        # 检查弹窗是否出现
        # wait.until(EC.alert_is_present())
        # 找到弹窗
        alert = self.driver.switch_to.alert
        # 判断一下弹窗里面的内容是否是确定删除？
        actual = alert.text
        print(actual)
        assert actual == "确定删除?"
        # 点击确定删除按钮
        alert.accept()
        time.sleep(1)
        BlogDriver.GetScreenShot()
        # 删除后判断一下是否返回到了博客列表页
        time.sleep(1)
        rettitle = self.driver.title
        retUrl = self.driver.current_url
        assert rettitle == "博客列表页"
        assert retUrl == "http://8.137.19.140:9090/blog_list.html"
        print("rettitle:" + rettitle)
        print("retUrl:" + retUrl)
        print("删除博客成功")
        self.driver.quit()
